﻿#include "VirtualPetHead"
#include <fstream>
#include <iostream>
#include <string>     // std::string, std::stof

const int max_stage = 6;


M_iP players;
M_sf stores;
M_iB bosses;
MySQLManager mysqlManager = MySQLManager();
//M_iP *playersPtr=new M_iP();
//M_sf *storesPtr=new M_sf();
//M_iB *bossesPtr=new M_iB();
Shop shop();

int main(int argc, char const *argv[])
{
    using namespace std;
    cout << "Create mysqlManager SEC\n";
    MYSQL *myc = mysqlManager.myCont;
    cout << "Create myc SEC\n";
    Database db = Database(myc, 3306);
    cout << "Create Table:\n";
    Table players_table = Table(myc, "players");
    Table bosses_table = Table(myc, "bosses");
    Table bags_table = Table(myc, "bags");
    cout << "USE DB\n";
    mysqlManager.UseDB(&db);
    cout << "CHANGE TABLE C\n";
    db.ChangePlayersTable(&players_table);
    db.ChangeBossesTable(&bosses_table);
    db.ChangeBagsTable(&bags_table);
    cout << "EPOLL\n";
    epoll_main();
    return 0;
}
#pragma region 消息处理（新）
std::string DealMessage(int fd, std::string recv_msg)
{
    //std::string recv_account=recv_msg.substr(0,recv_msg.find(':'));//得出账号·
    NetPackage *msgPack = new NetPackage(recv_msg);
    msgPack->Decrypt();

    //获取
    if (msgPack->Command() == "GET")
    {        
        //获取商店信息
        if (msgPack->ForText() == "Shop")
        {
            return shop.shopStr;
        }

        //键
        std::string key = msgPack->ForText().substr(msgPack->ForText().find("_") + 1);
        
        //获取玩家信息
        if (msgPack->ForText().at(0) == 'P')
        {
            //获取信息：账号
            if (key == PLAYERINFO_KEY_Account)
            {
                return players[fd].account;
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Passwd)
            {
                return players[fd].passwd;
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Money)
            {
                return std::to_string(players[fd].money);
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Pet)
            {
                return players[fd].pet;
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Hungry)
            {
                return std::to_string(players[fd].hungry);
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Play)
            {
                return std::to_string(players[fd].play);
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Train)
            {
                return std::to_string(players[fd].train);
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_LastOfflineTime)
            {
                return std::to_string(players[fd].lastOfflineTime);
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Health)
            {
                return std::to_string(players[fd].health);
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Attack)
            {
                return std::to_string(players[fd].attack);
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Defend)
            {
                return std::to_string(players[fd].defend);
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Dodge)
            {
                return std::to_string(players[fd].dodge);
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Crit)
            {
                return std::to_string(players[fd].crit);
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Level)
            {
                return std::to_string(players[fd].level);
            }
            //获取信息：
            if (key == PLAYERINFO_KEY_Stage)
            {
                return std::to_string(players[fd].stage);
            }
        }
        
        //获取背包信息
        else if(msgPack->ForText().at(0)=='b'){            
            //获取信息：
            if (key == PLAYERINFOBAG_KEY_ChangeName)
            {
                return std::to_string(players[fd].bag.changeName);
            }
            //获取信息：
            if (key == PLAYERINFOBAG_KEY_Hungry)
            {
                return std::to_string(players[fd].bag.hungry);
            }
            //获取信息：
            if (key == PLAYERINFOBAG_KEY_Play)
            {
                return std::to_string(players[fd].bag.play);
            }
            //获取信息：
            if (key == PLAYERINFOBAG_KEY_Train)
            {
                return std::to_string(players[fd].bag.train);
            }
            //获取信息：
            if (key == PLAYERINFOBAG_KEY_Health)
            {
                return std::to_string(players[fd].bag.health);
            }
            //获取信息：
            if (key == PLAYERINFOBAG_KEY_Attack)
            {
                return std::to_string(players[fd].bag.attack);
            }
            //获取信息：
            if (key == PLAYERINFOBAG_KEY_Defend)
            {
                return std::to_string(players[fd].bag.defend);
            }
            //获取信息：
            if (key == PLAYERINFOBAG_KEY_Dodge)
            {
                return std::to_string(players[fd].bag.dodge);
            }
            //获取信息：
            if (key == PLAYERINFOBAG_KEY_Crit)
            {
                return std::to_string(players[fd].bag.crit);
            }            
        }

        //获取BOSS信息
        else if(msgPack->ForText().at(0)=='B'){   
            int stage=std::stoi(msgPack->CONTENT());         
            //获取信息：
            if (key == BOSSINFO_KEY_Stage)
            {
                return std::to_string(bosses[stage].stage);
            }
            //获取信息：
            if (key == BOSSINFO_KEY_Health)
            {
                return std::to_string(bosses[stage].health);
            }
            //获取信息：
            if (key == BOSSINFO_KEY_Attack)
            {
                return std::to_string(bosses[stage].attack);
            }
            //获取信息：
            if (key == BOSSINFO_KEY_Defend)
            {
                return std::to_string(bosses[stage].defend);
            }
            //获取信息：
            if (key == BOSSINFO_KEY_Dodge)
            {
                return std::to_string(bosses[stage].dodge);
            }
            //获取信息：
            if (key == BOSSINFO_KEY_Crit)
            {
                return std::to_string(bosses[stage].crit);
            }   
        }
    
    }

    //设置
    else if (msgPack->Command() == "SET")
    {
        //设置商店信息
        if (msgPack->ForText() == "Shop")
        {
            return "Permission denied!";
        }
        
        //键
        std::string key = msgPack->ForText().substr(msgPack->ForText().find("_") + 1);
        //值
        std::string value = msgPack->CONTENT();
        
        //设置玩家信息
        if (msgPack->ForText().at(0) == 'P')
        {
            //设置信息：
            if (key == PLAYERINFO_KEY_Account)
            {
                return "Permission denied";
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Passwd)
            {
                players[fd].passwd=value;
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Money)
            {
                players[fd].money=std::stof(value);
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Pet)
            {
                players[fd].pet=value;
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Hungry)
            {
                players[fd].hungry=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Play)
            {
                players[fd].play=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Train)
            {
                players[fd].train=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_LastOfflineTime)
            {
                players[fd].lastOfflineTime=std::stof(value);
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Health)
            {
                players[fd].health=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Attack)
            {
                players[fd].attack=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Defend)
            {
                players[fd].defend=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Dodge)
            {
                players[fd].dodge=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Crit)
            {
                players[fd].crit=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Level)
            {
                players[fd].level=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFO_KEY_Stage)
            {
                players[fd].stage=std::stoi(value);
            }
            //日志
            return players[fd].account+" SET: "+key+"="+value;
        }
        //设置背包信息
        else if(msgPack->ForText().at(0)=='b'){
            //设置信息：
            if (key == PLAYERINFOBAG_KEY_ChangeName)
            {
                players[fd].bag.changeName=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFOBAG_KEY_Hungry)
            {
                players[fd].bag.hungry=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFOBAG_KEY_Play)
            {
                players[fd].bag.play=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFOBAG_KEY_Train)
            {
                players[fd].bag.train=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFOBAG_KEY_Health)
            {
                players[fd].bag.health=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFOBAG_KEY_Attack)
            {
                players[fd].bag.attack=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFOBAG_KEY_Defend)
            {
                players[fd].bag.defend=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFOBAG_KEY_Dodge)
            {
                players[fd].bag.dodge=std::stoi(value);
            }
            //设置信息：
            if (key == PLAYERINFOBAG_KEY_Crit)
            {
                players[fd].bag.crit=std::stoi(value);
            }
            return players[fd].account+"SET BAG: "+key+"="+value;
        }
    }

    //登录
    else if (msgPack->Command() == "Login")
    {
        //玩家fd不存在
        if (players.find(fd) == players.end())
        {
            AddPlayer(fd);
        }
        //存在,设置账号
        players[fd].account = msgPack->Account();
        //更新服务端信息
        UpdatePlayerInfo(fd);
        std::string ret = "money:" + std::to_string(players[fd].money) + "pet:" + players[fd].pet + "hungry:" + std::to_string(players[fd].hungry) + "play:" + std::to_string(players[fd].play) + "train:" + std::to_string(players[fd].train) + "health:" + std::to_string(players[fd].health) + "attack:" + std::to_string(players[fd].attack) + "defend:" + std::to_string(players[fd].defend) + "dodge:" + std::to_string(players[fd].dodge) + "crit:" + std::to_string(players[fd].crit) + "level:" + std::to_string(players[fd].level) + "stage:" + std::to_string(players[fd].stage);
        return ret;
    }
    
    //注册
    else if (msgPack->Command() == "Regist")
    {
        //玩家fd不存在
        if (players.find(fd) == players.end())
        {
            AddPlayer(fd);
        }
        //存在,设置账号
        players[fd].account = msgPack->Account();
        int res = mysqlManager.DBptr->players_table->AddPlayer(msgPack->Account().c_str(), msgPack->CONTENT().c_str());
        if (res == 0)
        {
            //更新服务端信息
            UpdatePlayerInfo(fd);
            std::string ret = "money:" + std::to_string(players[fd].money) + "pet:" + players[fd].pet + "hungry:" + std::to_string(players[fd].hungry) + "play:" + std::to_string(players[fd].play) + "train:" + std::to_string(players[fd].train) + "health:" + std::to_string(players[fd].health) + "attack:" + std::to_string(players[fd].attack) + "defend:" + std::to_string(players[fd].defend) + "dodge:" + std::to_string(players[fd].dodge) + "crit:" + std::to_string(players[fd].crit) + "level:" + std::to_string(players[fd].level) + "stage:" + std::to_string(players[fd].stage);
            return ret;
        }
        else if (res == -1)
        {
            return "Account is now in existence";
        }
    }

    return "Command Error";
}
std::string DealMessage(int fd,std::string recv_msg, std::string *send_msg)
{
    *send_msg=DealMessage(fd,recv_msg);
    return *send_msg;
}

#pragma endregion

/*commandOption可选项：(弃用)
    0. 未知指令
    1. 客户端请求登录           { "指令选项;账号;密码" }
    2. 客户端请求获取值         { "指令选项;账号;键" }
    3. 客户端请求修改密码       { "指令选项;账号;密码" }
    4. 客户端请求修改宠物名     { "指令选项;账号;新宠物名" }
    5. 客户端请求修改金钱额度   { "指令选项;账号;值" }
    6. 客户端请求修改宠物饥饿度 { "指令选项;账号;值" }
    7. 客户端请求修改宠物快乐值 { "指令选项;账号;值" }
    8. 客户端请求修改宠物训练值 { "指令选项;账号;值" }
    9. 客户端请求修改HP值       { "指令选项;账号;值" }
    10. 客户端请求修改宠物攻击值  { "指令选项;账号;值" }
    11. 客户端请求修改宠物防御值  { "指令选项;账号;值" }
    12. 客户端请求修改宠物闪避值  { "指令选项;账号;值" }
    13. 客户端请求修改宠物暴击值  { "指令选项;账号;值" }
    14. 客户端请求修改宠物等级值  { "指令选项;账号;值" }
    14. 客户端请求修改关卡值      { "指令选项;账号;值" }
    87. 客户端请求获取Boss信息    { "指令选项;关卡;键" }
    88. 客户端请求注册           { "指令选项;账号;密码" }
*/

#pragma region 弃用
//收到消息结构：{ "指令选项;账号;信息" }
// int DealCommand(char *calResult, char *reqFromClnt)
// {
//     using namespace std;
//     int commandOption, checkResult;
//     char *account;
//     char *info;
//     //============分割字符串===========
//     commandOption = atoi(strtok(reqFromClnt, ";")); //取第一项，指令值
//     account = strtok(NULL, ";");                    //继续分割取第二项，账号
//     info = strtok(NULL, ";");                       //第三项，信息主体
//     cout<<"来自 {"<<account<<"} -> "<<reqFromClnt<<endl;
//     //=============根据指令进行运算===============
//     switch (commandOption)
//     {
//     //未知指令
//     case 0:
//         cout << "未知指令：" << commandOption << ",来自" << account << endl;
//         strcpy(calResult,"99;Illegal instruction");
//         break;
//     //客户端请求登录
//     case 1:
//         checkResult =  mysqlManager.DBptr->players_table->CheckPasswd(account, info);
//         //判断密码是否正确
//         switch (checkResult)
//         {
//         case 0: //回传两次登录时间差
//         {
//             char timeStr[16];
//              mysqlManager.DBptr->players_table->GetValueFromPlayers(account, "lastOfflineTime", timeStr);
//             long long t = atoll(timeStr);
//             int div = time(NULL) - t;
//             sprintf(calResult, "0;Password is correct,Time difference: %d", div);
//             cout << "账号登录，时间差： " << div << endl;
//              mysqlManager.DBptr->players_table->ChangeValue(account,"lastOfflineTime",time(NULL));
//         }
//         break;
//         case -1:
//             strcpy(calResult, "1;Wrong password");
//             break;
//         case 1:
//             strcpy(calResult, "2;Account does not exist");
//             break;
//         default:
//             cout << "账号 " << account << " 请求登录出现未知错误,请求时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             strcpy(calResult, "98;Unknown mistake");
//             break;
//         }
//         break;

//     //客户端请求获取值
//     case 2:
//         checkResult =  mysqlManager.DBptr->players_table->GetValueFromPlayers(account, info, calResult);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             char temp[128];
//             sprintf(temp,calResult);
//             sprintf(calResult,"0;%s",temp);
//             cout << "账号 " << account << " 请求获取" << info << "成功，请求时间: " << time(NULL) << endl
//                  << "回复值：" << calResult << endl;
//             break;
//         case -1:
//             strcpy(calResult, "3;Query error");
//             break;
//         case 1:
//             strcpy(calResult, "2;Account does not exist");
//             break;
//         default:
//             cout << "账号 " << account << " 请求获取" << info << "失败，请求时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             strcpy(calResult, "98;Unknown mistake");
//             break;
//         }
//         break;

//     //客户端请求修改密码
//     case 3:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "passwd", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了密码,新密码: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改密码失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             cout << "账号 " << account << " 修改密码失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             strcpy(calResult, "98;Unknown mistake");
//             break;
//         }
//         break;

//     //客户端请求修改宠物名
//     case 4:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "pet", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了宠物名,新名称: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改宠物名失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             cout << "账号 " << account << " 修改宠物名失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             strcpy(calResult, "98;Unknown mistake");
//             break;
//         }
//         break;

//     //客户端请求修改金钱额度
//     case 5:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "money", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了金钱为: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改金钱失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             strcpy(calResult, "98;Unknown mistake");
//             cout << "账号 " << account << " 修改金钱失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             break;
//         }
//         break;

//     //客户端请求修改宠物饥饿度
//     case 6:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "hungry", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了宠物饥饿度为: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改宠物饥饿度失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             strcpy(calResult, "98;Unknown mistake");
//             cout << "账号 " << account << " 修改宠物饥饿度失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             break;
//         }
//         break;

//     //客户端请求修改宠物快乐值
//     case 7:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "play", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了宠物快乐值为: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改宠物快乐值失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             strcpy(calResult, "98;Unknown mistake");
//             cout << "账号 " << account << " 修改宠物快乐值失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             break;
//         }
//         break;

//     //客户端请求修改宠物训练值
//     case 8:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "train", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了宠物训练值为: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改宠物训练值失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             strcpy(calResult, "98;Unknown mistake");
//             cout << "账号 " << account << " 修改宠物训练值失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             break;
//         }
//         break;

//     //客户端请求修改宠物HP值
//     case 9:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "health", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了宠物HP值为: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改宠物HP值失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             strcpy(calResult, "98;Unknown mistake");
//             cout << "账号 " << account << " 修改宠物HP值失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             break;
//         }
//         break;

//     //客户端请求修改宠物攻击值
//     case 10:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "attack", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了宠物attack值为: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改宠物攻击值失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             strcpy(calResult, "98;Unknown mistake");
//             cout << "账号 " << account << " 修改宠物attack值失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             break;
//         }
//         break;

//     //客户端请求修改宠物防御值
//     case 11:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "defend", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了宠物adefend值为: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改宠物防御值失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             strcpy(calResult, "98;Unknown mistake");
//             cout << "账号 " << account << " 修改宠物defend值失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             break;
//         }
//         break;

//     //客户端请求修改宠物闪避值
//     case 12:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "dodge", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了宠物dodge值为: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改宠物闪避值失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             strcpy(calResult, "98;Unknown mistake");
//             cout << "账号 " << account << " 修改宠物dodge值失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             break;
//         }
//         break;

//     //客户端请求修改宠物暴击值
//     case 13:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "crit", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了宠物crit值为: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改宠物暴击值失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             strcpy(calResult, "98;Unknown mistake");
//             cout << "账号 " << account << " 修改宠物crit值失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             break;
//         }
//         break;

//     //客户端请求修改宠物等级值
//     case 14:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "level", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了宠物level值为: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改宠物等级值失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             strcpy(calResult, "98;Unknown mistake");
//             cout << "账号 " << account << " 修改宠物level值失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             break;
//         }
//         break;

//     //客户端请求修改关卡值
//     case 15:
//         checkResult =  mysqlManager.DBptr->players_table->ChangeValue(account, "stage", info);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Successfully modified");
//             cout << "账号 " << account << " 成功修改了宠物stage值为: " << info << "修改时间: " << time(NULL) << endl;
//             break;
//         case 99:
//             strcpy(calResult, "2;Account does not exist");
//             cout << "账号 " << account << " 不存在，请求修改关卡值失败: " << "请求时间: " << time(NULL) << endl;
//             break;
//         default:
//             strcpy(calResult, "98;Unknown mistake");
//             cout << "账号 " << account << " 修改宠物stage值失败，修改时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             break;
//         }
//         break;

//     //客户端请求获取Boss信息
//     case 87:
//         //PS：此处account项为Boss中的stage字段键，info为要获取的键
//         checkResult =  mysqlManager.DBptr->bosses_table->GetValueFromBosses(account, info, calResult);
//         //判断是否正常
//         switch (checkResult)
//         {
//         case 0:
//             char temp[128];
//             sprintf(temp,calResult);
//             sprintf(calResult,"0;%s",temp);
//             cout << "某账号 请求获取 boss 信息 成功，请求时间: " << time(NULL) << endl
//                  <<"关卡："<<account<<" ,属性: "<<info << " ,回复值：" << calResult << endl;
//             break;
//         case -1:
//             strcpy(calResult, "3;Query error");
//             break;
//         case 1:
//             strcpy(calResult, "2;Account does not exist");
//             break;
//         default:
//            cout << "某账号 请求获取 boss 信息 失败，请求时间: " << time(NULL) << endl
//                 <<"关卡："<<account<<" ,属性: "<<info << endl
//                 << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             strcpy(calResult, "98;Unknown mistake");
//             break;
//         }
//         break;
//     //客户端请求注册
//     case 88:
//         checkResult =  mysqlManager.DBptr->players_table->AddPlayer(account, info);
//         //判断注册是否成功
//         switch (checkResult)
//         {
//         case 0:
//             strcpy(calResult, "0;Registered successfully");
//             cout<<"账号 {"<<account<<"}注册成功，密码为: "<<info<<endl;
//             break;
//         case -1:
//             strcpy(calResult, "4;Account already in existence");
//             break;
//         default:
//             cout << "账号 " << account << " 请求注册出现未知错误,请求时间: " << time(NULL) << endl
//                  << "{ " << commandOption << "," << account << "," << info << "}" << endl;
//             strcpy(calResult, "98;Unknown mistake");
//             break;
//         }
//         break;

//     //客户端其他指令
//     default:
//         cout << "未知指令：" << commandOption << ",来自" << account << endl;
//         strcpy(calResult,"99;Illegal instruction");
//         break;
//     }

//     cout<<"回复 {"<<account<<"} -> "<<calResult<<endl;
//     checkResult = 0;
//     return 0;
// }

#pragma endregion

#pragma region 新函数

void AddPlayer(int fd)
{
    PlayerInfo newPlayer = PlayerInfo();
    players.insert(M_iP::value_type(fd, newPlayer));
}

void PlayerLogin(int fd, std::string account, std::string passwd)
{
    // char temp[64];
    // int isCorrect= mysqlManager.DBptr->players_table->CheckPasswd(account.c_str(),passwd.c_str());
    // if(isCorrect==0){
    //     players[fd].passwd=passwd;
    //      mysqlManager.DBptr->players_table
    //     ->GetValueFromPlayers(account.c_str(),"money",temp);
    //     players[fd].money=atof(temp);
    // }
    //设置账号
    players[fd].account = account;
    if (players[fd].passwd == passwd && (!players[fd].isLogined))
    {
        players[fd].isLogined = true;
        //更新服务端信息
        UpdatePlayerInfo(fd);
    }
}

void DeletePlayer(int fd)
{
    players.erase(fd);
}

//保存
int Save(int fd)
{
    std::string acc = players[fd].account;
    if ( //玩家数据保存是否成功
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "money", players[fd].money) == 0 &&
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "pet", players[fd].pet.c_str()) == 0 &&
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "hungry", players[fd].hungry) == 0 &&
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "play", players[fd].play) == 0 &&
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "train", players[fd].train) == 0 &&
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "health", players[fd].health) == 0 &&
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "attack", players[fd].attack) == 0 &&
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "defend", players[fd].defend) == 0 &&
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "dodge", players[fd].dodge) == 0 &&
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "crit", players[fd].crit) == 0 &&
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "level", players[fd].level) == 0 &&
        mysqlManager.DBptr->players_table->ChangeValue(acc.c_str(), "stage", players[fd].stage) == 0)
    {
        std::cout << "Player: " << acc << "Save Success!" << std::endl;
    }
    else
    {
        std::cout << "Player Save Error!!,Acc:" << acc << std::endl;
        std::ofstream SaveErrPlayers;
        SaveErrPlayers.open("SaveErrorPlayers.err");
        SaveErrPlayers << "account:" << players[fd].account << std::endl
                       << "passwd:" << players[fd].passwd << std::endl
                       << "pet:" << players[fd].pet << std::endl
                       << "hungry:" << players[fd].hungry << std::endl
                       << "play:" << players[fd].play << std::endl
                       << "train:" << players[fd].train << std::endl
                       << "health:" << players[fd].health << std::endl
                       << "attack:" << players[fd].attack << std::endl
                       << "defend:" << players[fd].defend << std::endl
                       << "dodge:" << players[fd].dodge << std::endl
                       << "crit:" << players[fd].crit << std::endl
                       << "level:" << players[fd].level << std::endl
                       << "stage:" << players[fd].stage << std::endl
                       << std::endl
                       << std::endl;
        SaveErrPlayers.close();
    }
    if ( //数据保存是否成功
        mysqlManager.DBptr->bags_table->ChangeValue(acc.c_str(), "changeName", players[fd].bag.changeName) == 0 &&
        mysqlManager.DBptr->bags_table->ChangeValue(acc.c_str(), "hungry", players[fd].bag.hungry) == 0 &&
        mysqlManager.DBptr->bags_table->ChangeValue(acc.c_str(), "play", players[fd].bag.play) == 0 &&
        mysqlManager.DBptr->bags_table->ChangeValue(acc.c_str(), "train", players[fd].bag.train) == 0 &&
        mysqlManager.DBptr->bags_table->ChangeValue(acc.c_str(), "health", players[fd].bag.health) == 0 &&
        mysqlManager.DBptr->bags_table->ChangeValue(acc.c_str(), "attack", players[fd].bag.attack) == 0 &&
        mysqlManager.DBptr->bags_table->ChangeValue(acc.c_str(), "defend", players[fd].bag.defend) == 0 &&
        mysqlManager.DBptr->bags_table->ChangeValue(acc.c_str(), "dodge", players[fd].bag.dodge) == 0 &&
        mysqlManager.DBptr->bags_table->ChangeValue(acc.c_str(), "crit", players[fd].bag.crit) == 0)
    {
        std::cout << "Player: " << acc << "'s Bag Save Success!" << std::endl;
    }
    else
    {
        std::cout << "Player's Bag Save Error!!,{\nAcc:" << acc << std::endl;
        //文件输出流
        std::ofstream SaveErrPlayers;
        //打开文件
        SaveErrPlayers.open("SaveErrorPlayers.err");
        //写入
        SaveErrPlayers << "changeName:" << players[fd].bag.changeName << std::endl
                       << "hungry:" << players[fd].bag.hungry << std::endl
                       << "play:" << players[fd].bag.play << std::endl
                       << "train:" << players[fd].bag.train << std::endl
                       << "health:" << players[fd].bag.health << std::endl
                       << "attack:" << players[fd].bag.attack << std::endl
                       << "defend:" << players[fd].bag.defend << std::endl
                       << "dodge:" << players[fd].bag.dodge << std::endl
                       << "crit:" << players[fd].bag.crit << "\n}" << std::endl
                       << std::endl
                       << std::endl;
        //关闭
        SaveErrPlayers.close();
    }
}

//玩家信息更新
int UpdatePlayerInfo(int fd)
{
    std::string account = players[fd].account;
    players[fd].passwd = mysqlManager.DBptr->players_table->GetValue("account", account, "passwd");
    players[fd].attack = std::stoi(mysqlManager.DBptr->players_table->GetValue("account", account, "attack"));
    players[fd].crit = std::stoi(mysqlManager.DBptr->players_table->GetValue("account", account, "crit"));
    players[fd].defend = std::stoi(mysqlManager.DBptr->players_table->GetValue("account", account, "defend"));
    players[fd].dodge = std::stoi(mysqlManager.DBptr->players_table->GetValue("account", account, "dodge"));
    players[fd].health = std::stoi(mysqlManager.DBptr->players_table->GetValue("account", account, "health"));
    players[fd].hungry = std::stoi(mysqlManager.DBptr->players_table->GetValue("account", account, "hungry"));
    players[fd].level = std::stoi(mysqlManager.DBptr->players_table->GetValue("account", account, "level"));
    players[fd].money = std::stof(mysqlManager.DBptr->players_table->GetValue("account", account, "money"));
    players[fd].pet = mysqlManager.DBptr->players_table->GetValue("account", account, "pet");
    players[fd].play = std::stoi(mysqlManager.DBptr->players_table->GetValue("account", account, "play"));
    players[fd].stage = std::stoi(mysqlManager.DBptr->players_table->GetValue("account", account, "stage"));
    players[fd].train = std::stoi(mysqlManager.DBptr->players_table->GetValue("account", account, "train"));
    players[fd].bag.attack = std::stoi(mysqlManager.DBptr->bags_table->GetValue("account", account, "attack"));
    players[fd].bag.crit = std::stoi(mysqlManager.DBptr->bags_table->GetValue("account", account, "crit"));
    players[fd].bag.defend = std::stoi(mysqlManager.DBptr->bags_table->GetValue("account", account, "defend"));
    players[fd].bag.dodge = std::stoi(mysqlManager.DBptr->bags_table->GetValue("account", account, "dodge"));
    players[fd].bag.health = std::stoi(mysqlManager.DBptr->bags_table->GetValue("account", account, "health"));
    players[fd].bag.hungry = std::stoi(mysqlManager.DBptr->bags_table->GetValue("account", account, "hungry"));
    players[fd].bag.changeName = std::stoi(mysqlManager.DBptr->bags_table->GetValue("account", account, "changeName"));
    players[fd].bag.play = std::stoi(mysqlManager.DBptr->bags_table->GetValue("account", account, "play"));
    players[fd].bag.train = std::stoi(mysqlManager.DBptr->bags_table->GetValue("account", account, "train"));
}
//Boss信息更新
int UpdateBossInfo()
{
    bosses.clear();
    B newBoss = BossInfo();
    for (int i = 0; i < max_stage; i++)
    {
        newBoss.attack = std::stoi(mysqlManager.DBptr->bosses_table->GetValue("stage", std::to_string(i), "attack"));
        newBoss.crit = std::stoi(mysqlManager.DBptr->bosses_table->GetValue("stage", std::to_string(i), "crit"));
        newBoss.defend = std::stoi(mysqlManager.DBptr->bosses_table->GetValue("stage", std::to_string(i), "defend"));
        newBoss.dodge = std::stoi(mysqlManager.DBptr->bosses_table->GetValue("stage", std::to_string(i), "dodge"));
        newBoss.health = std::stoi(mysqlManager.DBptr->bosses_table->GetValue("stage", std::to_string(i), "health"));
        newBoss.stage = std::stoi(mysqlManager.DBptr->bosses_table->GetValue("stage", std::to_string(i), "stage"));
        bosses.insert(M_iB::value_type(i, newBoss));
    }
}

#pragma endregion
